<?php
defined('BY_JYA') or exit('error');
global $_RQ,$_AR;
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
$return=array();
if($_RQ['op']=='list'){
	$params=array('uniacid'=>$_SESSION['uniacid']);
	if($_RQ['title']){
		$params['title like']='%'.trim($_RQ['title']).'%';
		$return['params']['title']=$_RQ['title'];
	}
	$orderby=array();
	$pindex = max(1, intval($_RQ['page']));
	$psize = 10;
	$total='';
	$list=pdo_getslice('core_account_message',$params,array($pindex,$psize),$total,$fields,'','id desc');
	$return['list']=$list?$list:array();
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$htmls=array(
		'type'=>'list',
		'tabs'=>array(
			array('op'=>'list','title'=>'评论列表'),
			array('op'=>'formlist','title'=>'自定义表单列表'),
		),
		'list'=>array('data'=>'list','edit'=>true,'list'=>array(
			array('name'=>'id','title'=>'ID','type'=>'string'),
			array('name'=>'nickname','title'=>'姓名','type'=>'string'),
			array('name'=>'logo','title'=>'电话','type'=>'image'),
			array('name'=>'content','title'=>'留言内容','type'=>'string'),
			array('name'=>'createtime','title'=>'创建时间','type'=>'string'),
			array('name'=>'status','title'=>'状态','type'=>'switch','set'=>'set'),
		)),
		'search_one'=>array('name'=>'username','help'=>'客户姓名','add'=>false),
	);
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='edit'){
	$id=intval($_RQ['id']);
	if($id){
		$item=pdo_get('core_account_message',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
		$item['createtime']=date('Y/m/d H:i:s',$item['createtime']);
	}else{
		$item=array();
	}
	$return['item']=$item;
	$htmls=array(
		'type'=>'edit',
		'post'=>'post',
		'desc'=>'留言',
		'tabs'=>array(
			array('op'=>'list','title'=>'留言列表'),
			array('op'=>'formlist','title'=>'自定义表单列表'),
		),
		'edit'=>array(
			array('type'=>'copy','title'=>'留言姓名','name'=>'nickname', 'help'=>'' ),
			array('type'=>'copy','title'=>'头像','name'=>'logo', 'help'=>'' ),
			array('type'=>'copy','title'=>'内容','name'=>'content', 'help'=>'' ),
			array('type'=>'copy','title'=>'留言时间','name'=>'createtime', 'help'=>'' ),
			array('type'=>'switch','title'=>'状态','name'=>'status', 'help'=>'' ),
		)
	);
	$return['htmls']=$htmls;
	set_html('framess',$this->menu());
	exi($return);
}
if($_RQ['op']=='post'){
	$postdata=get_postdata();
	$data=array(
		'status'=>trim($postdata['status']),
		'content'=>trim($postdata['content'])
	);
	$id=intval($postdata['id']);
	if($id){
		pdo_update('core_account_message',$data,array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
		exi('留言修改成功！');
	}else{
		exi('没有找到相关留言');
	}
}
if($_RQ['op']=='set'){
	$post=get_postdata();
	$id=intval($_RQ['id']);
	pdo_update('core_account_message',array('status'=>intval($post['status'])),array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	exi('操作成功！');
}
if($_RQ['op']=='del'){
	$id=intval($_RQ['id']);
	if($id){
		pdo_delete('core_account_message',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	}
	$del=trim($_RQ['del'],';');
	if($del){
		$del=explode(';',$del);
		foreach($del as $id){
			$id=intval($id);
			if($id){
				pdo_delete('core_account_message',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
			}
		}
	}
	exi('删除成功！');
}
if($_RQ['op']=='formlist'){
	$params=array();
	$params['uniacid']=$_SESSION['uniacid'];
	if(!empty($_RQ['nickname'])){
		$params['nickname like']='%'.trim($_RQ['nickname']).'%';
		$return['params']['nickname']=$_RQ['nickname'];
	}
	$pindex = max(1, intval($_RQ['page']));
	$psize = 10;
	$total='';
	$list=pdo_getslice('core_account_message',$params,array($pindex,$psize),$total,$fields,'','id desc');
	if($list){
		foreach($list as &$item){
			$member=pdo_get('core_members',array('id'=>$item['uid'],'uniacid'=>$_SESSION['uniacid']),array('avatar','nickname'));
			$item['logo'] = $member['avatar'];
			$item['nickname'] = $member['nickname'];
		}
	}
	$return['list']=$list;
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$htmls=array(
		'type'=>'list',
		'tabs'=>array(
			array('op'=>'list','title'=>'留言列表'),
			array('op'=>'formlist','title'=>'自定义表单列表'),
		),
		'list'=>array('data'=>'list','edit'=>false,'list'=>array(
			array('name'=>'nickname','title'=>'客户名称','type'=>'string'),
			array('name'=>'logo','title'=>'客户头像','type'=>'image'),
			array('name'=>'createtime','title'=>'留言时间','type'=>'time'),
		),'action'=>array(
			array('type'=>'href','op'=>'look','title'=>'留言详情'),
			)
		),
		'search_one'=>array('name'=>'nickname','help'=>'客户名称','add'=>false),
	);
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='look'){
	$return=array();
	$id=intval($_RQ['id']);
	if($id){
		$item=pdo_get('core_account_message',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	}else{
		exi('请选择留言','error');
	}
	
	$member=pdo_get('core_members',array('id'=>$item['uid'],'uniacid'=>$_SESSION['uniacid']),array('avatar','nickname'));
	$item['avatar'] = $member['avatar'];
	$item['nickname'] = $member['nickname'];
	$item['data']=$item['data']?json_decode($item['data'],true):array();
	$formtypes=get_form_types();
	
	foreach($item['data'] as &$v){
		if($v['type']==5 && $v['data']){
			$v['data'] = explode(";", $v['data']);
			foreach($v['data'] as $k=>&$i){
				if(empty($v['data'][$k])){
					unset($v['data'][$k]);
				}
			}
		}
		$v['type']=$formtypes[$v['type']];
	}
	$return['item']=$item;
	$htmls=array(
		'type'=>'look',
		'tabs'=>array(
			array('op'=>'list','title'=>'返回项目列表'),
		),
		'desc'=>'留言信息',
		'look'=>array(
				array('type'=>'string','title'=>'用户名称','name'=>'nickname','help'=>''),
				array('type'=>'image','title'=>'用户头像','name'=>'avatar','help'=>''),
				array(
						'title'=>'表单详情',
						'type'=>'array',
						'name'=>'data',
						'list'=>array(
							array('type'=>'string','name'=>'title','title'=>'表单标题'),	
							array('type'=>'labelarray','name'=>'data','title'=>'表单内容'),	
						),
				)
		)
	);
	$return['htmls']=$htmls;
	exi($return);
}